package com.sec.key.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.sec.base.utils.AjaxJson;
import com.sec.key.dao.KeyDao;
import com.sec.key.entity.Key;
import com.sec.key.service.KeyService;
import com.sec.user.entity.User;

@Service("keyService")
public class KeyServiceImpl implements KeyService {

	@Autowired
	private KeyDao keyDao;

	@Override
	public AjaxJson findKeyListByPage(HttpServletRequest request, int pageNumber, int pageSize) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取分页数据失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
			Page<Key> page = keyDao.findAllByUser(user, pageable);
			Map<String, Object> pagaData = new HashMap<>();
			pagaData.put("keyList", page.getContent());
			pagaData.put("count", page.getTotalElements());
			j.setData(pagaData);
			j.setCode("success");
			j.setMsg("获取分页数据成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson saveOrUpdate(HttpServletRequest request, Key key) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("操作失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			key.setUser(user);
			keyDao.save(key);
			j.setCode("success");
			j.setMsg("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson getKeyInfo(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("查询密码信息失败");
		Optional<Key> optional = keyDao.findById(id);
		if (optional.isPresent()) {
			Key key = optional.get();
			j.setCode("success");
			j.setMsg("查询密码信息成功");
			j.setData(key);
		}
		return j;
	}

	@Override
	public AjaxJson deleteKey(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("删除密码失败");
		try {
			Optional<Key> optional = keyDao.findById(id);
			if (optional.isPresent()) {
				Key key = optional.get();
				keyDao.delete(key);
				j.setCode("success");
				j.setMsg("删除密码成功");
			} else {
				j.setMsg("删除密码数据异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson findAllKeyByLoginUser(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取密码数据失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			List<Key> keys = keyDao.findAllByUser(user);
			j.setData(keys);
			j.setCode("success");
			j.setMsg("获取密码数据成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}
}
